package com.cyss.greenball.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;

import com.cyss.greenball.dao.SaveMsgDao;
import com.cyss.greenball.main.R;
import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.lianzt.util.StringUtil;

public class ActivityUtil {
	public static final int TOAST = 20;
	public static final int DIALOG = 21;
	public static final int SAVEMAP_WITH_NAME = 22;

	public static final int ACTIVITY_MODE_FINISH = 30;
	public static final int ACTIVITY_MODE_RESUME = 31;

	public static void Toast(Context context, AbstractCommonData acd) {
		String text = acd.getStringValue("text");
		if (StringUtil.isNull(text)) {
			return;
		}
		int duration = Toast.LENGTH_LONG;
		try {
			duration = acd.getIntValue("duration");
		} catch (Exception e) {
		}
		Toast.makeText(context, text, duration).show();
	}

	public static void Dialog(Context context, AbstractCommonData acd) {
		String text = acd.getStringValue("text");
		if (StringUtil.isNull(text)) {
			return;
		}
		String button1 = acd.getStringValue("button1");
		if (StringUtil.isNull(button1)) {
			return;
		}
		DialogInterface.OnClickListener button1ClickListener = (DialogInterface.OnClickListener) acd
				.getObjectValue("button1_listener");
		if (button1ClickListener == null) {
			button1ClickListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			};
		}
		String button2 = acd.getStringValue("button2");
		if (StringUtil.isNull(button2)) {
			return;
		}
		DialogInterface.OnClickListener button2ClickListener = (DialogInterface.OnClickListener) acd
				.getObjectValue("button2_listener");
		if (button2ClickListener == null) {
			button2ClickListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			};
		}
		String title = acd.getStringValue("title");
		if (StringUtil.isNull(title)) {
			title = context.getString(R.string.game_tip);
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(text);
		builder.setPositiveButton(button1, button1ClickListener);
		builder.setNegativeButton(button2, button2ClickListener);
		builder.show();
	}

	public static void saveMapWithName(final Context context,
			final AbstractCommonData acd) {
		final EditText et = new EditText(context);
		new AlertDialog.Builder(context)
				.setTitle(R.string.save_tip)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setView(et)
				.setPositiveButton(context.getString(R.string.save),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								final String saveName = et.getText().toString();
								try {
									if (StringUtil.isNull(saveName)) {
										Toast.makeText(
												context,
												saveName
														+ context
																.getString(R.string.need_map_name),
												Toast.LENGTH_LONG).show();
									} else {
										if (SaveMsgDao.getValue(context,
												saveName) == null) {
											SaveMsgDao.putValue(context,
													saveName, acd);
											Toast.makeText(
													context,
													saveName
															+ context
																	.getString(R.string.save_success),
													Toast.LENGTH_LONG).show();
										} else {
											AbstractCommonData recoverData = DataConvertFactory
													.getInstanceEmpty();
											recoverData
													.putStringValue(
															"text",
															context.getString(R.string.map_recovery));
											recoverData.putStringValue(
													"button1",
													context.getString(R.string.cancle));
											recoverData.putStringValue(
													"button2",
													context.getString(R.string.make_sure));
											recoverData
													.putObjectValue(
															"button2_listener",
															new DialogInterface.OnClickListener() {

																@Override
																public void onClick(
																		DialogInterface dialog,
																		int which) {
																	SaveMsgDao
																			.putValue(
																					context,
																					saveName,
																					acd);
																	Toast.makeText(
																			context,
																			saveName
																					+ " "
																					+ context
																							.getString(R.string.save_success),
																			Toast.LENGTH_LONG)
																			.show();
																}
															});
											Dialog(context, recoverData);
										}
									}
								} catch (Exception e) {
									Toast.makeText(
											context,
											saveName
													+ context
															.getString(R.string.save_fail)
													+ " :" + e.toString(),
											Toast.LENGTH_LONG).show();
								}
							}
						})
				.setNegativeButton(context.getString(R.string.cancle), null)
				.show();
	}

}
